package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* SelectDesignSurfaceView
* */
class SelectDesignSurfaceView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_2)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            pathEffect = CornerPathEffect(mStrokeWidth)
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_17DC38)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            strokeWidth = mStrokeWidth
            pathEffect = CornerPathEffect(mStrokeWidth)
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_17DC38)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy + (cy / 5 - cy * 2 / 5) / 2)

            val baseX = cx * 3 / 5
            val baseY = cy * 3 / 5
            mPath.apply {
                reset()
                moveTo(-baseX, 0F)
                lineTo(0F, -baseY)
                lineTo(baseX, 0F)
                lineTo(0F, baseY)
                close()
            }
            drawPath(mPath, mPaint0)

            mPath.apply {
                reset()
                moveTo(-cx * 3 / 5, cy * 6 / 25)
                lineTo(0F, cy * 4 / 5)
                lineTo(cx * 3 / 5, cy * 6 / 25)
            }
            drawPath(mPath, mPaint1)
        }
    }
}